"use strict"
/**
 * @name bind
 * 账号绑定
 */


/**
 * @name list
 * @param string guid 玩家GUID
 * 绑定列表
 */
exports.list = function() {
    let guid = this.get('guid','string');
    if ( !guid  ) {
        return this.error('args empty');
    }
    let query = {"guid":guid};
    let option = {multi:true,dataType:"array",fields:{ 'sdk':1, 'guid':1} };
    let openidModel = this.model.branch("openid",this.branch);
    return openidModel.find(query,option)
}



/**
 * @name submit
 * @param string access1 当前游戏使用SDK的授权
 * @param string access2 需要绑定的新SDK的授权
 * 绑定账号
 */
exports.submit = function() {
    let access1 = this.get('access1','string');
    let access2 = this.get('access2','string');
    if ( !access1 || !access2 ) {
        return this.error('args empty');
    }
    if(access1 == access2){
        return this.error("access error")
    }

    let guid;
    let query = {"access":{"$in":[access1,access2] } };
    let option = {"multi":true,"fields":{ '_id':1, 'guid':1,'access':1,'gzone':1},"key":"access" };
    let openidModel = this.model.branch("openid",this.branch);
    return openidModel.find(query,option).then((ret)=>{
        if(!ret[access1]){
            return this.error("access_not_exist",access1);
        }
        if(!ret[access2]){
            return this.error("access_not_exist",access2);
        }
        guid = ret[access1]['guid'];

        if(guid === ret[access2]['_id']){
            return this.error('guid_bind_error',guid); //不能绑定自身
        }

        if(ret[access2]['_id'] !== ret[access2]['guid']){
            return this.error('guid_bind_exist',ret[access2]['_id'],ret[access2]['guid'] ); //账号已经绑定其他角色
        }
        else if(Array.isArray(ret[access2]['gzone']) && ret[access2]['gzone'].length > 0){
            return this.error('guid_role_exist',ret[access2]['gzone'] );                     //账号已经创建过角色，无法绑定
        }

        return ret[access2]['_id'];
    }).then(id=>{
        //检查是否被其他账号绑定
        let query = {"guid":id,"_id":{"$ne":id} };
        return openidModel.count(query).then(ret=>{
            if(ret > 0){
                return this.error("openid_bound",ret); //此账号被其他SDK绑定为主账号，不能再作为子账号绑定到其他账户
            }
            else {
                return id;
            }
        })
    }).then(id=>{
        //执行绑定操作
        return openidModel.set(id,"guid",guid);
    })

}



/**
 * @name remove
 * @param string access 当前需要解除绑定的登录凭证
 * 解除绑定
 * 如果已经创建角色，则会失去原有角色
 */
exports.remove = function() {
    let access = this.get('access','string');
    if ( !access ) {
        return this.error('access empty');
    }
    let query = {"access":access};
    let option = {multi:false,fields:{"guid":1} };
    let openidModel = this.model.branch("openid",this.branch);
    return openidModel.find(query,option).then(ret=>{
        if(!ret){
            return this.error('access_not_exist',access); //未绑定
        }
        let id = ret['_id'];
        // if( id=== ret['guid'] ){
        //     return this.error('bind_id_error',id); //主账号不能解绑
        // }
        let d= {"guid":id,"gzone":[]}
        return openidModel.set(id,d);    //解绑，变成主账号
    });

}